package com.sg.service.biz.framework.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.sg.common.util.RedisUtil;
import com.sg.dto.base.framework.req.BatchQueryCodeByCodeReq;
import com.sg.dto.base.framework.req.QueryCodeListReq;
import com.sg.dto.base.framework.req.QueryTypeDetailReq;
import com.sg.dto.base.framework.req.QueryTypeListReq;
import com.sg.dto.biz.framework.req.*;
import com.sg.dto.biz.framework.res.*;
import com.sg.entity.MetadataCode;
import com.sg.entity.MetadataType;
import com.sg.service.base.framework.MMetadataCodeService;
import com.sg.service.base.framework.MMetadataTypeService;
import com.sg.common.exception.Assert;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class TypeCodeServiceImpl
        implements com.wicket.okrframework.biz.service.TypeCodeService {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private MMetadataTypeService mMetadataTypeService;
    @Resource
    private MMetadataCodeService mMetadataCodeService;

    /**
     * D1-1查询类型列表[284]
     * gen by moon at 10/24/2022, 9:43:51 PM
     */
    @Trace(operationName = "D1-1查询类型列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryTypeListRespDto queryTypeList(QueryTypeListReqDto reqDto) {


        List<MetadataType> listMetadataType_1 = new ArrayList<>();
//步骤0: 1-1-05查询类型列表 - queryTypeList
        List<MetadataType> listMetadataType = new ArrayList<>();
        QueryTypeListReq queryTypeListReq = new QueryTypeListReq();
        if (reqDto != null) {
            queryTypeListReq.setTypeName(reqDto.getTypeName());//SimpleFieldAssign//sourceId:4769_1
            queryTypeListReq.setBizAppCode(reqDto.getBizAppCode());//SimpleFieldAssign//sourceId:388687_1
            queryTypeListReq.setBizSceneCode(reqDto.getBizSceneCode());//SimpleFieldAssign//sourceId:388688_1
            queryTypeListReq.setIsArchive(reqDto.getIsArchive());//SimpleFieldAssign//sourceId:15155_1
            queryTypeListReq.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:9981_1
        }

        /*1-1-05查询类型列表[221]   */

        listMetadataType = mMetadataTypeService.queryTypeList(queryTypeListReq);


        listMetadataType_1 = listMetadataType;

        QueryTypeListRespDto retData = new QueryTypeListRespDto();
        retData.setBaseMetadataTypeList(listMetadataType_1.stream().map(item -> BeanUtil.toBean(item, BaseMetadataTypeDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:4791_1


        return retData;
    }

    /**
     * D1-1查询编码列表[648]
     * gen by moon at 8/10/2022, 3:24:42 AM
     */
    @Trace(operationName = "D1-1查询编码列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryCodeListRespDto queryCodeList(QueryCodeListReqDto reqDto) {


        List<MetadataCode> listMetadataCode_1 = new ArrayList<>();
//步骤0: 1-1-13查询编码列表 - queryCodeList
        List<MetadataCode> listMetadataCode = new ArrayList<>();
        QueryCodeListReq queryCodeListReq = new QueryCodeListReq();
        if (reqDto != null) {
            queryCodeListReq.setTypeId(reqDto.getTypeId());//sourceId:10983_1
            queryCodeListReq.setTypeCode(reqDto.getTypeCode());//sourceId:10984_1
            queryCodeListReq.setCodeName(reqDto.getCodeName());//sourceId:10985_1
            queryCodeListReq.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//sourceId:10994_1
            queryCodeListReq.setIsArchive(reqDto.getIsArchive());//sourceId:15159_1
        }

        /*1-1-13查询编码列表[649]   */

        listMetadataCode = mMetadataCodeService.queryCodeList(queryCodeListReq);

        listMetadataCode_1 = listMetadataCode;

        QueryCodeListRespDto retData = new QueryCodeListRespDto();
        retData.setTypeEncodingList(listMetadataCode_1.stream().map(item -> BeanUtil.toBean(item, TypeEncodingDto.class)).collect(Collectors.toList()));//sourceId:11000_1


        return retData;
    }

    /**
     * D1-1查询类型详情[282]
     * gen by moon at 8/10/2022, 3:21:42 AM
     */
    @Trace(operationName = "D1-1查询类型详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryTypeDetailRespDto queryTypeDetail(QueryTypeDetailReqDto reqDto) {


        MetadataType metadataType_1 = null;
//步骤0: 1-1-05查询类型详情 - queryTypeDetail
        MetadataType metadataType = null;
        QueryTypeDetailReq queryTypeDetailReq = new QueryTypeDetailReq();
        if (reqDto != null) {
            queryTypeDetailReq.setTypeId(reqDto.getTypeId());//sourceId:9890_1
            queryTypeDetailReq.setTypeCode(reqDto.getTypeCode());//sourceId:9891_1
        }

        /*1-1-05查询类型详情[24]   */

        metadataType = mMetadataTypeService.queryTypeDetail(queryTypeDetailReq);

        metadataType_1 = metadataType;

        QueryTypeDetailRespDto retData = new QueryTypeDetailRespDto();
        if (metadataType_1 != null) {
            retData.setTypeId(metadataType_1.getTypeId());//sourceId:9913_1
            retData.setTypeCode(metadataType_1.getTypeCode());//sourceId:9914_1
            retData.setTypeName(metadataType_1.getTypeName());//sourceId:9915_1
            retData.setSubjectLifeCycle(metadataType_1.getSubjectLifeCycle());//sourceId:9924_1
            retData.setOperationInductionId(metadataType_1.getOperationInductionId());//sourceId:9918_1
            retData.setOperateTime(metadataType_1.getOperateTime());//sourceId:9919_1
        }


        return retData;
    }

    /**
     * D1-1新增编码[519]
     * gen by moon at 8/17/2022, 8:01:34 PM
     */
    @Trace(operationName = "D1-1新增编码")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AddCodeRespDto addCode(AddCodeReqDto reqDto) {


        String string_1 = null;
//步骤0: 1-1-13新增编码 - addCode
        String string = null;
        MetadataCode metadataCode = new MetadataCode();
        if (reqDto != null) {
            metadataCode.setTypeId(reqDto.getTypeId());//sourceId:10511_1
            metadataCode.setTypeCode(reqDto.getTypeCode());//sourceId:23643_1
            metadataCode.setCode(reqDto.getCode());//sourceId:7760_1
            metadataCode.setCodeName(reqDto.getCodeName());//sourceId:7761_1
            metadataCode.setOrderNumber(reqDto.getOrderNumber());//sourceId:7762_1
            metadataCode.setIsPushCache(reqDto.getIsPushCache());//sourceId:193641_1
            metadataCode.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//sourceId:14563_1
        }

        /*1-1-13新增编码[89]   */
        Assert.isNull(metadataCode.getTypeId(), "D1-1新增编码-1-1-13新增编码-类型ID不能为空", false);
        Assert.isNull(metadataCode.getTypeCode(), "D1-1新增编码-1-1-13新增编码-类型的编码不能为空", false);
        Assert.isNull(metadataCode.getCode(), "D1-1新增编码-1-1-13新增编码-编码不能为空", false);
        Assert.isNull(metadataCode.getCodeName(), "D1-1新增编码-1-1-13新增编码-编码名称不能为空", false);
        Assert.isNull(metadataCode.getOrderNumber(), "D1-1新增编码-1-1-13新增编码-排序不能为空", false);
        Assert.isNull(metadataCode.getSubjectLifeCycle(), "D1-1新增编码-1-1-13新增编码-主体生命周期不能为空", false);
        string = mMetadataCodeService.addCode(metadataCode);

        string_1 = string;

        AddCodeRespDto retData = new AddCodeRespDto();
        if (string_1 != null) {
            retData.setCodeId(string_1);//sourceId:28322_1
        }


        return retData;
    }

    /**
     * D1-1修改编码[517]
     * gen by moon at 8/10/2022, 3:23:39 AM
     */
    @Trace(operationName = "D1-1修改编码")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public UpdateCodeRespDto updateCode(UpdateCodeReqDto reqDto) {


        //步骤0: 1-1-13修改编码 - updateCode
        boolean bOOLEAN;
        MetadataCode metadataCode = new MetadataCode();
        if (reqDto != null) {
            metadataCode.setCodeId(reqDto.getCodeId());//sourceId:7739_1
            metadataCode.setCode(reqDto.getCode());//sourceId:7741_1
            metadataCode.setCodeName(reqDto.getCodeName());//sourceId:7742_1
            metadataCode.setOrderNumber(reqDto.getOrderNumber());//sourceId:7743_1
            metadataCode.setIsPushCache(reqDto.getIsPushCache());//sourceId:193647_1
            metadataCode.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//sourceId:14587_1
        }

        /*1-1-13修改编码[90]   */
        Assert.isNull(metadataCode.getCodeId(), "D1-1修改编码-1-1-13修改编码-编码ID不能为空", false);
        bOOLEAN = mMetadataCodeService.updateCode(metadataCode);


        UpdateCodeRespDto retData = new UpdateCodeRespDto();


        return retData;
    }

    /**
     * D1-1修改类型[279]
     * gen by moon at 8/10/2022, 3:21:41 AM
     */
    @Trace(operationName = "D1-1修改类型")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public UpdateTypeRespDto updateType(UpdateTypeReqDto reqDto) {


        //步骤0: 1-1-05修改类型 - updateType
        boolean bOOLEAN;
        MetadataType metadataType = new MetadataType();
        if (reqDto != null) {
            metadataType.setTypeId(reqDto.getTypeId());//sourceId:4584_1
            metadataType.setTypeCode(reqDto.getTypeCode());//sourceId:4585_1
            metadataType.setTypeName(reqDto.getTypeName());//sourceId:4586_1
            metadataType.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//sourceId:14708_1
        }

        /*1-1-05修改类型[22]   */

        bOOLEAN = mMetadataTypeService.updateType(metadataType);


        UpdateTypeRespDto retData = new UpdateTypeRespDto();


        return retData;
    }

    /**
     * D1-1新增类型[270]
     * gen by moon at 10/25/2022, 9:51:51 AM
     */
    @Trace(operationName = "D1-1新增类型")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AddTypeRespDto addType(AddTypeReqDto reqDto) {


        String string_1 = null;
//步骤0: 1-1-05新增类型 - addType
        String string = null;
        MetadataType metadataType = new MetadataType();
        if (reqDto != null) {
            metadataType.setTypeCode(reqDto.getTypeCode());//SimpleFieldAssign//sourceId:4201_1
            metadataType.setTypeName(reqDto.getTypeName());//SimpleFieldAssign//sourceId:4202_1
            metadataType.setBizAppCode(reqDto.getBizAppCode());//SimpleFieldAssign//sourceId:388675_1
            metadataType.setBizSceneCode(reqDto.getBizSceneCode());//SimpleFieldAssign//sourceId:388676_1
            metadataType.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:14712_1
        }

        /*1-1-05新增类型[21]   */
        Assert.isNull(metadataType.getTypeCode(), "D1-1新增类型-1-1-05新增类型-类型的编码不能为空", false);
        Assert.isNull(metadataType.getTypeName(), "D1-1新增类型-1-1-05新增类型-类型名称不能为空", false);
        Assert.isNull(metadataType.getBizAppCode(), "D1-1新增类型-1-1-05新增类型-归属业务应用标识不能为空", false);
        Assert.isNull(metadataType.getBizSceneCode(), "D1-1新增类型-1-1-05新增类型-归属业务场景标识不能为空", false);
        Assert.isNull(metadataType.getSubjectLifeCycle(), "D1-1新增类型-1-1-05新增类型-主体生命周期不能为空", false);
        string = mMetadataTypeService.addType(metadataType);


        string_1 = string;

        AddTypeRespDto retData = new AddTypeRespDto();
        if (string_1 != null) {
            retData.setTypeId(string_1);//SimpleFieldAssign//sourceId:28326_1
        }


        return retData;
    }

    /**
     * D1-1批量查询编码列表[1142]
     */
    @Trace(operationName = "D1-1批量查询编码列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public BatchQueryCodeListRespDto batchQueryCodeList(BatchQueryCodeListReqDto reqDto) {


        //步骤0: 1-1-13批量查询编码列表 - batchQueryCodeList
//        Object unknown_1 = null;
//        Object unknown = new Object();
//        //todo dong 未找到匹配生成策略,请检查生成策略unknown=reqDto;,数据源项; to( ==>tableName:null, fieldEnname:typeList ,uniqueId: 23650_1 uniqueSourceId:23658_1) from (varName:undefined fieldreqDto)
//
//        /*1-1-13批量查询编码列表[1143]   */
//        unknown_1 = mMetadataCodeService.batchQueryCodeList(unknown);


        BatchQueryCodeListRespDto retData = new BatchQueryCodeListRespDto();
        //todo dong 未找到匹配生成策略,请检查生成策略retData.setTypeEncodingList(unknown_1.getCodeId()/unknown_1.getCode()/unknown_1.getCodeName()/unknown_1.getOrderNumber()),数据源项; to( ==>tableName:null, fieldEnname:typeEncodingList ,uniqueId: 23655_1 uniqueSourceId:out_null_null) from (varName:undefined fieldundefined)


        return retData;
    }

    /**
     * D1-1查询编码列表(公共)[648]
     * gen by moon at 10/16/2022, 1:02:19 AM
     */
    @Trace(operationName = "D1-1查询编码列表(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryCodeListComRespDto queryCodeListCom(QueryCodeListComReqDto reqDto) {


        List<MetadataCode> listMetadataCode_1 = new ArrayList<>();
//步骤0: 1-1-13查询编码列表 - queryCodeList
        List<MetadataCode> listMetadataCode = new ArrayList<>();
        QueryCodeListReq queryCodeListReq = new QueryCodeListReq();
        if (reqDto != null) {
            queryCodeListReq.setTypeId(reqDto.getTypeId());//SimpleFieldAssign//sourceId:10983_1
            queryCodeListReq.setTypeCode(reqDto.getTypeCode());//SimpleFieldAssign//sourceId:10984_1
            queryCodeListReq.setCode(reqDto.getCode());//SimpleFieldAssign//sourceId:222592_1
            queryCodeListReq.setCodeName(reqDto.getCodeName());//SimpleFieldAssign//sourceId:10985_1
            queryCodeListReq.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:10994_1
            queryCodeListReq.setIsArchive(reqDto.getIsArchive());//SimpleFieldAssign//sourceId:15159_1
        }

        /*1-1-13查询编码列表[649]   */

        listMetadataCode = mMetadataCodeService.queryCodeList(queryCodeListReq);


        listMetadataCode_1 = listMetadataCode;

        QueryCodeListComRespDto retData = new QueryCodeListComRespDto();
        retData.setTypeEncodingList(listMetadataCode_1.stream().map(item -> BeanUtil.toBean(item, TypeEncodingDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:11000_1


        return retData;
    }

    /**
     * D1-1查询类型详情(公共)[282]
     * gen by moon at 10/15/2022, 5:39:14 PM
     */
    @Trace(operationName = "D1-1查询类型详情(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryTypeDetailComRespDto queryTypeDetailCom(QueryTypeDetailComReqDto reqDto) {


        MetadataType metadataType_1 = null;
//步骤0: 1-1-05查询类型详情 - queryTypeDetail
        MetadataType metadataType = null;
        QueryTypeDetailReq queryTypeDetailReq = new QueryTypeDetailReq();
        if (reqDto != null) {
            queryTypeDetailReq.setTypeId(reqDto.getTypeId());//SimpleFieldAssign//sourceId:9890_1
            queryTypeDetailReq.setTypeCode(reqDto.getTypeCode());//SimpleFieldAssign//sourceId:9891_1
            queryTypeDetailReq.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:219498_1
        }

        /*1-1-05查询类型详情[24]   */

        metadataType = mMetadataTypeService.queryTypeDetail(queryTypeDetailReq);


        metadataType_1 = metadataType;

        QueryTypeDetailComRespDto retData = new QueryTypeDetailComRespDto();
        if (metadataType_1 != null) {
            retData.setTypeId(metadataType_1.getTypeId());//SimpleFieldAssign//sourceId:9913_1
            retData.setTypeCode(metadataType_1.getTypeCode());//SimpleFieldAssign//sourceId:9914_1
            retData.setTypeName(metadataType_1.getTypeName());//SimpleFieldAssign//sourceId:9915_1
            retData.setSubjectLifeCycle(metadataType_1.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:9924_1
            retData.setOperationInductionId(metadataType_1.getOperationInductionId());//SimpleFieldAssign//sourceId:9918_1
            retData.setOperateTime(metadataType_1.getOperateTime());//SimpleFieldAssign//sourceId:9919_1
        }


        return retData;
    }

    /**
     * D1批量查询编码byCode(公共)[5300]
     * gen by moon at 11/26/2022, 6:14:57 PM
     */
    @Trace(operationName = "D1批量查询编码byCode(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public BatchQueryCodeByCodeComRespDto batchQueryCodeByCodeCom(BatchQueryCodeByCodeComReqDto reqDto) {


        List<MetadataCode> listMetadataCode_1 = new ArrayList<>();
//步骤0: 1-1-13批量查询编码byCode - batchQueryCodeByCode
        List<MetadataCode> listMetadataCode = new ArrayList<>();
        BatchQueryCodeByCodeReq batchQueryCodeByCodeReq = new BatchQueryCodeByCodeReq();
        if (reqDto != null) {
            batchQueryCodeByCodeReq.setCodeByCodeList(reqDto.getCodeByCodeList());//list-field-assign//sourceId:458978_1
            batchQueryCodeByCodeReq.setTypeCode(reqDto.getTypeCode());//SimpleFieldAssign//sourceId:458980_1
            batchQueryCodeByCodeReq.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:458979_1
        }

        /*1-1-13批量查询编码byCode[5299]   */
        Assert.isNull(batchQueryCodeByCodeReq.getTypeCode(), "D1批量查询编码byCode(公共)-1-1-13批量查询编码byCode-类型的编码不能为空", false);
        Assert.isNull(batchQueryCodeByCodeReq.getSubjectLifeCycle(), "D1批量查询编码byCode(公共)-1-1-13批量查询编码byCode-主体生命周期不能为空", false);
        listMetadataCode = mMetadataCodeService.batchQueryCodeByCode(batchQueryCodeByCodeReq);
        Assert.isTrue(listMetadataCode == null || CollectionUtil.isEmpty(listMetadataCode) || listMetadataCode == null || CollectionUtil.isEmpty(listMetadataCode) || listMetadataCode.size() == 0, "找不到数据，系统异常", false);

        listMetadataCode_1 = listMetadataCode;

        BatchQueryCodeByCodeComRespDto retData = new BatchQueryCodeByCodeComRespDto();
        retData.setCodeByCodeList(listMetadataCode_1.stream().map(item -> BeanUtil.toBean(item, CodeByCodeDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:458985_1


        return retData;
    }
    //
}
